#!/usr/bin/env python3

class Data:
    def match_structure(self, entry):
        # if arbitrary XML resembles our data structures
        # (which it does if it exhibits the same keys)
        entry_keys = entry.keys()
        for key in self.keys:
            # specified keys not allowed to be missing
            if key not in entry_keys:
                return False

            # at this point, we process the actual content as well
            # so as to avoid restreaming for actual processing
            payload = entry[key]
            # this checks for any post processing methods to apply to payload
            if self.post[key] != 0:
                payload = self.post[key](payload)
            # collecting that data
            self.data[key] = payload
        return True

    def __str__(self):
        str_repr = ""
        for key in self.keys:
            str_repr += "{k}: {v}\n".format(k=key, v=self.data[key])
        return str_repr

def post_process_status(payload):
    if not isinstance(payload, str):
        return payload
    return payload.lower()

def post_process_comments(payload):
    if payload is None:
        return ''
    if not isinstance(payload, str):
        return payload
    return payload.replace('=== MAL Tags ===\n', '')

class Summary(Data):
    def __init__(self):
        self.data = {}
        # done as a way to "generically" add post processing methods
        # like below for anime->my_comments
        self.post = { 'user_name': 0, 'user_export_type': 0, 'user_total_anime': 0, 'user_total_watching': 0, 'user_total_completed': 0, 'user_total_onhold': 0, 'user_total_dropped': 0, 'user_total_plantowatch': 0 }
        self.keys = self.post.keys()

class Anime(Data):
    def __init__(self):
        self.data = {}
        self.post = { 'series_title': 0, 'series_episodes': 0, 'my_watched_episodes': 0, 'my_start_date': 0, 'my_finish_date': 0, 'my_score': 0, 'my_status': post_process_status, 'my_comments': post_process_comments }
        self.keys = self.post.keys()